package com.mj.mafeed.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.mj.mafeed.bean.ProductJSON;
import com.mj.mafeed.bean.ProductListJSON;
import com.mj.mafeed.util.DBHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 产品Dao
 * @author zhaominglei
 * @date 2014-12-11
 * 
 */
public class ProductDao extends BaseDao {
	private static final String TABLE_NAME = "feed_product";
	
	public int getCount(Context context) {
		SQLiteDatabase db = DBHelper.getInstance(context).getReadableDatabase();
		Cursor cursor = db.rawQuery("select count(*) from "+TABLE_NAME, null);
		Long count = null;
		if (cursor.moveToFirst()) {
			count = cursor.getLong(0);
		}
		cursor.close();
		return (count != null) ? count.intValue() : 0;
	}
	
	public void save(Context context, ProductJSON productJSON) {
		SQLiteDatabase db = DBHelper.getInstance(context).getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("productid", productJSON.getProductId());
		contentValues.put("sn", productJSON.getSn());
		contentValues.put("name", productJSON.getName());
		contentValues.put("price", productJSON.getPrice().doubleValue());
		contentValues.put("stock", productJSON.getStock());
		contentValues.put("imageurl", productJSON.getImageUrl());
		contentValues.put("memo", productJSON.getMemo());
		contentValues.put("introduction", productJSON.getIntroduction());
		contentValues.put("issell", productJSON.getIsSell()?1:0);
		contentValues.put("weight", productJSON.getWeight());
		db.insert(TABLE_NAME, null, contentValues);
	}
	
	public void delete(Context context, Long productId) {
		SQLiteDatabase  db = DBHelper.getInstance(context).getWritableDatabase();
		db.execSQL("delete from "+TABLE_NAME+" where productid=?", new Object[]{ productId });
	}
	
	public void deleteAll(Context context) {
		deleteAll(context, TABLE_NAME);
	}
	
	public void update(Context context, ProductJSON productJSON) {
		SQLiteDatabase db = DBHelper.getInstance(context).getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("productid", productJSON.getProductId());
		contentValues.put("sn", productJSON.getSn());
		contentValues.put("name", productJSON.getName());
		contentValues.put("price", productJSON.getPrice().doubleValue());
		contentValues.put("stock", productJSON.getStock());
		contentValues.put("imageurl", productJSON.getImageUrl());
		contentValues.put("memo", productJSON.getMemo());
		contentValues.put("introduction", productJSON.getIntroduction());
		contentValues.put("issell", productJSON.getIsSell()?1:0);
		contentValues.put("weight", productJSON.getWeight());
		db.update(TABLE_NAME, contentValues, null, null);
	}
	
	public ProductJSON getById(Context context, Long productId) {
		ProductJSON productJSON = new ProductJSON();
		SQLiteDatabase  db = DBHelper.getInstance(context).getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from "+TABLE_NAME+" where productid=?", new String[]{ String.valueOf(productId) });
		if (cursor.moveToFirst()) {
			productJSON.setProductId(cursor.getLong(cursor.getColumnIndex("productid")));
			productJSON.setSn(cursor.getString(cursor.getColumnIndex("sn")));
			productJSON.setName(cursor.getString(cursor.getColumnIndex("name")));
			productJSON.setPrice(new BigDecimal(cursor.getDouble(cursor.getColumnIndex("price"))));
			productJSON.setStock(cursor.getInt(cursor.getColumnIndex("stock")));
			productJSON.setImageUrl(cursor.getString(cursor.getColumnIndex("imageurl")));
			productJSON.setMemo(cursor.getString(cursor.getColumnIndex("memo")));
			productJSON.setIntroduction(cursor.getString(cursor.getColumnIndex("introduction")));
			productJSON.setIsSell(cursor.getInt(cursor.getColumnIndex("issell")) == 1?true:false);
			productJSON.setWeight(cursor.getInt(cursor.getColumnIndex("weight")));
		}
		cursor.close();
		return productJSON;
	}
	
	public ProductListJSON getAll(Context context) {
		ProductListJSON productListJSON = new ProductListJSON();
		SQLiteDatabase db = DBHelper.getInstance(context).getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
		List<ProductJSON> recordList = new ArrayList<ProductJSON>();
		while (cursor.moveToNext()) {
			ProductJSON productJSON = new ProductJSON();
			productJSON.setProductId(cursor.getLong(cursor.getColumnIndex("productid")));
			productJSON.setSn(cursor.getString(cursor.getColumnIndex("sn")));
			productJSON.setName(cursor.getString(cursor.getColumnIndex("name")));
			productJSON.setPrice(new BigDecimal(cursor.getDouble(cursor.getColumnIndex("price"))));
			productJSON.setStock(cursor.getInt(cursor.getColumnIndex("stock")));
			productJSON.setImageUrl(cursor.getString(cursor.getColumnIndex("imageurl")));
			productJSON.setMemo(cursor.getString(cursor.getColumnIndex("memo")));
			productJSON.setIntroduction(cursor.getString(cursor.getColumnIndex("introduction")));
			productJSON.setIsSell(cursor.getInt(cursor.getColumnIndex("issell")) == 1?true:false);
			productJSON.setWeight(cursor.getInt(cursor.getColumnIndex("weight")));
			recordList.add(productJSON);
		}
		productListJSON.setRecordList(recordList);
		cursor.close();
		return productListJSON;
	}
	
	public ProductListJSON getPagination(Context context, String selection, String[] selectionArgs, String orderBy, int currentPage, int pageSize) {
		ProductListJSON productListJSON = new ProductListJSON();
		SQLiteDatabase db = DBHelper.getInstance(context).getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, selection, selectionArgs, null, null, orderBy, (currentPage - 1) * pageSize+","+pageSize);
		List<ProductJSON> recordList = new ArrayList<ProductJSON>();
		while (cursor.moveToNext()) {
			ProductJSON productJSON = new ProductJSON();
			productJSON.setProductId(cursor.getLong(cursor.getColumnIndex("productid")));
			productJSON.setSn(cursor.getString(cursor.getColumnIndex("sn")));
			productJSON.setName(cursor.getString(cursor.getColumnIndex("name")));
			productJSON.setPrice(new BigDecimal(cursor.getDouble(cursor.getColumnIndex("price"))));
			productJSON.setStock(cursor.getInt(cursor.getColumnIndex("stock")));
			productJSON.setImageUrl(cursor.getString(cursor.getColumnIndex("imageurl")));
			productJSON.setMemo(cursor.getString(cursor.getColumnIndex("memo")));
			productJSON.setIntroduction(cursor.getString(cursor.getColumnIndex("introduction")));
			productJSON.setIsSell(cursor.getInt(cursor.getColumnIndex("issell")) == 1?true:false);
			productJSON.setWeight(cursor.getInt(cursor.getColumnIndex("weight")));
			recordList.add(productJSON);
		}
		
		int recordCount = getCount(context);
        int currPage = currentPage;
        if(currentPage < 1) {
            currentPage = 1;
        }
        boolean hasPrePage = false;
        //上一页等于当前页减一
        int prePage = currPage - 1;
        if(prePage < 1) {
            hasPrePage = false;//没有上一页
            prePage = 1;
        }else {
            hasPrePage = true;//有上一页
        }
        //计算总页数
        int pageCount = (int)Math.ceil(recordCount / (double)pageSize);
        if(currPage > pageCount) {
        	currPage = pageCount;
        }
        boolean hasNextPage = false;
        //下一页等于当前页加一
        int nextPage = currPage + 1;
        if(nextPage > pageCount) {
        	hasNextPage = false;//没有下一页
            nextPage = pageCount;
        }else {
        	hasNextPage = true;//有下一页
        }
        
        productListJSON.setRecordCount(recordCount);
		productListJSON.setPageSize(pageSize);
        productListJSON.setCurrentPage(currPage);
        productListJSON.setHasNextPage(hasNextPage);
        productListJSON.setHasPrePage(hasPrePage);
        productListJSON.setNextPage(nextPage);
        productListJSON.setPageCount(pageCount);
        productListJSON.setPrePage(prePage);
		productListJSON.setRecordList(recordList);
		cursor.close();
		return productListJSON;
	}
}